package com.guanzhi.bean;

import com.guanzhi.aop.service.CalculatorService;
import com.guanzhi.di.Book;
import com.guanzhi.service.UserService;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Locale;

/**
 * Spring 6
 * <p> Package: com.guanzhi.bean </p>
 * <p> File: AppBeanContextText.java </p>
 * <p> Description:  (获取Bean对象 测试类)</p>
 * <p> Date: 2024/8/9 22:52</p>
 *
 * @author LiKai
 * @version 1.0 （代码版本）
 * @email <a href="mailto:Likai55634@aliyun.com">LiKai</a>（邮箱）
 * @date 2024/8/9 22:52
 * @since jdk1.8（jdk版本）
 */
public class AppBeanContextText {

    /**
     * 引入日志
     */
    private final Logger logger = LoggerFactory.getLogger(AppBeanContextText.class);

    @Test
    public void appContext() {
        //加载Spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //获取配置创建的对象
        User user = (User) context.getBean("user");
        //输出测试结果
        logger.info("user=={}", user);
    }

    /**
     * 根据ID方式获取Bean
     */
    @Test
    public void getUserServiceBean() {
        //加载Spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //获取配置创建的对象
        UserService userService = (UserService) context.getBean("userService");

        System.out.println(userService.getUserName());
    }

    /**
     * 根据类型获取Bean
     */
    @Test
    public void getUserServiceTypeBean() {
        //加载Spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //获取配置创建的对象
        UserService userService = context.getBean(UserService.class);

        System.out.println(userService.getUserName());
    }

    /**
     * 根据ID和类型获取Bean
     */
    @Test
    public void getUserServiceAndTypeBean() {
        //加载Spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //获取配置创建的对象
        UserService userService = context.getBean("userService", UserService.class);

        System.out.println(userService.getUserName());
    }

    /**
     * 属性注入
     */
    @Test
    public void diPropertyInjectProperty() {
        //加载Spring配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-di.xml");
        //获取配置创建的对象
        Book book = applicationContext.getBean("bookProperty", Book.class);
        //输出测试结果
        System.out.println(book);
    }

    /**
     * 构造器注入
     */
    @Test
    public void diPropertyInjectConstructor() {
        //加载Spring配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-di.xml");
        //获取配置创建的对象
        Book book = applicationContext.getBean("bookConstructor", Book.class);
        //输出测试结果
        System.out.println(book);
    }

    @Test
    public void factoryBean() {
        //加载Spring配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-factoryBean.xml");
        //获取配置创建的对象
        User user = (User) applicationContext.getBean("myFactoryBean");
        System.out.println(user);
    }

    @Test
    public void testAop() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-aop.xml");
        CalculatorService calculatorService = applicationContext.getBean(CalculatorService.class);
        Integer add = calculatorService.add(1, 2);
        System.out.println(add);
    }

    @Test
    public void testI18n() {
        //加载Spring配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-i18n.xml");
        //获取国际化资源内容
        String message = applicationContext.getMessage("hello.words", null, Locale.CHINA);
        System.out.println(message);
    }
}
